﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.Security;
using System.Data;
using LMHTX.Modules.DIRECTORY_TEAM.Components;
using System.IO;
using ASPNETVN.PORTAL.Components.Share;

namespace LMHTX.Modules.DIRECTORY_TEAM.Controls
{
    public partial class AddEditItem : AddEditItemControl
    {
        protected override void OnInit(EventArgs e)
        {
            this.ButtonUpdate = btnUpdate;
            this.ButtonDelete = btnDelete;
            this.ButtonCancel = btnCancel;

            this.Update += AddEditItem_Update;
            this.Delete += AddEditItem_Delete;
            base.OnInit(e);
        }

        void AddEditItem_Delete(object sender, EventArgs e)
        {
            try
            {
                if (this.ItemID > 0)
                {
                    Components.Team.DeleteTeam(this.ItemID);
                    this.ReturnToReferrerPage();
                }
            }
            catch
            {
            }
        }

        void AddEditItem_Update(object sender, EventArgs e)
        {
            try
            {
                Components.Team team = new Components.Team();
                if (this.ItemID > 0)
                {
                    team = Components.Team.Load(this.ItemID);
                }
                int areaID = cbArea.SelectedValue == string.Empty ? 0 : Convert.ToInt32(cbArea.SelectedValue);
                int typeID = cbType.SelectedValue == string.Empty ? 0 : Convert.ToInt32(cbType.SelectedValue);
                int careerID = cbCareer.SelectedValue == string.Empty ? 0 : Convert.ToInt32(cbCareer.SelectedValue);
                int trainingID = cbTraining.SelectedValue == string.Empty ? 0 : Convert.ToInt32(cbTraining.SelectedValue);
                

                team.ModuleID = this.ModuleID;
                team.Title = txtTitle.Text;
                team.TypeID = typeID;
                team.CareerID = careerID;
                team.AreaID = areaID;
                team.TrainingID = trainingID;
                team.TeamLeader = txtTeamLeader.Text;             
                
                if (txtCharterCapital.Text.Trim().Length!=0)
                    team.CharterCapital = Double.Parse(txtCharterCapital.Text);
                if (txtWorkingCapital.Text.Trim().Length != 0)
                    team.WorkingCapital = Double.Parse(txtWorkingCapital.Text);
                if (txtFixedCapital.Text.Trim().Length != 0)
                    team.FixedCapital = Double.Parse(txtFixedCapital.Text);
                if (txtTurnover.Text.Trim().Length != 0)
                    team.Turnover = Double.Parse(txtTurnover.Text);
                if (txtProfit.Text.Trim().Length != 0)
                    team.Profit = Double.Parse(txtProfit.Text);
                if (txtLabour.Text.Trim().Length != 0)
                    team.LabourNum = Int32.Parse(txtLabour.Text);
                if (txtTeamMember.Text.Trim().Length !=0)
                    team.TeamMember = Int32.Parse(txtTeamMember.Text);
                team.Comment = HttpUtility.HtmlEncode(txtContent.Value);

                team.CreatedBy = PortalSecurity.CurrentUser.Identity.Email;
                if (this.ItemID == 0)
                {
                    team.CreatedDate = DateTime.Now;
                    this.ItemID = team.Insert();
                }
                else
                {
                    team.CreatedDate = team.CreatedDate;
                    team.ID = this.ItemID;
                    team.Update();
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            string url = HttpUrlBuilder.BuildAddEditUrl(this.ModuleID, 0, "TeamManager");
            Response.Redirect(url);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                cbArea.DataSource = Components.Area.SelectAll(this.ModuleID);
                cbArea.DataBind();

                cbCareer.DataSource = Components.Career.SelectAll(this.ModuleID);
                cbCareer.DataBind();

                cbTraining.DataSource = Components.Training.SelectAll(this.ModuleID);
                cbTraining.DataBind();

                cbType.DataSource = Components.Type.SelectAll(this.ModuleID);
                cbType.DataBind();

                lblCreationTime.Text = DateTime.Today.ToShortDateString();
                lblCreatedBy.Text = PortalSecurity.CurrentUser.Identity.Email;

                if (this.ItemID > 0)
                {
                    Components.Team team = Components.Team.Load(this.ItemID);
                    if (team != null)
                    {
                        cbArea.SelectedValue = team.AreaID.ToString();
                        cbCareer.SelectedValue = team.CareerID.ToString();
                        cbTraining.SelectedValue = team.TrainingID.ToString();
                        cbType.SelectedValue = team.TypeID.ToString();
                        txtTitle.Text = team.Title;
                        txtTeamLeader.Text = team.TeamLeader;
                        txtCharterCapital.Text = team.CharterCapital.ToString();
                        txtWorkingCapital.Text = team.WorkingCapital.ToString();
                        txtFixedCapital.Text = team.FixedCapital.ToString();
                        txtTurnover.Text = team.Turnover.ToString();
                        txtProfit.Text = team.Profit.ToString();
                        txtLabour.Text = team.LabourNum.ToString();
                        txtTeamMember.Text = team.TeamMember.ToString();
                        txtContent.Value = HttpUtility.HtmlDecode(team.Comment);

                        lblCreatedBy.Text = team.CreatedBy;
                        lblCreationTime.Text = team.CreatedDate.ToString();

                    }
                }
                else
                {
                    btnDelete.Visible = false;
                }
            }
        }
    }
}